﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;

using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;

namespace ZF.WS.shop
{
    public partial class cellect : ZF.BLL.Page.GMPage
    {
        [PageArgument(Key = "type", DefaultValue =null)]
        public string Type { get; set; }
        [PageArgument(Key = "id", DefaultValue = -1)]
        public int Mid { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if ("add".EqIgnoreCase(this.Type))
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var merc = dc.SP_Merchandise.FirstOrDefault(p => p.MerchandiseId == Mid && !p.IsDel);
                    if (merc == null)
                    {
                        throw WarnFactory.CreateWarn("未找到该商品信息，可能该商品已经被删除", WarnIcon.Error, WarnLink.GoDefault);
                    }
                    dc.SP_Cellect.InsertOnSubmit(new ZF.Model.SP_Cellect()
                    {
                        AddTime = DateTime.Now,
                        MemberId = this.CurrentUser.MemberId,
                        MerchandiseId = merc.MerchandiseId,

                    });
                    dc.SubmitChanges();
                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        Link = WarnLink.GoBack,
                        Message = "收藏成功",
                    }).Alert();
                    return;

                }
            }
            else
            {
                if (!IsPostBack)
                {
                    View();
                }
            }
        }
        public void View()
        {
            ListBind();
        }
        /// <summary>
        /// 列表绑定
        /// </summary>
        public void ListBind()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                List<Expression<Func<Model.SP_Cellect, bool>>> filterList = new List<Expression<Func<Model.SP_Cellect, bool>>>();
                IQueryable<Model.SP_Cellect> selector = dc.SP_Cellect;
                filterList.Add(p => p.MemberId == this.CurrentUser.MemberId);
                foreach (var filter in filterList)
                {
                    selector = selector.Where(filter);
                }
                var selWhere = selector.Select(p =>
                    new
                    {
                        Merc=p.SP_Merchandise,
                        Cellect=p,
                    });
                var selPage = selWhere.OrderBy(this.gvList.SortExpression)
                        .Skip(gvList.PageIndex * gvList.PageSize).Take(gvList.PageSize);
                this.gvList.RowCount = selWhere.Count();
                var source = selPage.ToList();
                this.gvList.DataSource = source;
                this.gvList.DataBind();

            }
        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListSorting(object sender, GridViewSortEventArgs e)
        {
            ListBind();
        }
        /// <summary>
        /// 换页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListPageChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvList.PageIndex = e.NewPageIndex;
            ListBind();
        }
        public void Delete(params int[] idArr)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {
                    dc.SP_Cellect.DeleteAllOnSubmit(
                       dc.SP_Cellect.Where(p => idArr.Contains(p.CellectId)));
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = "商品收藏";
                if (isSuccess)
                    SiteInfo.WarnFactory.CreateWarn(string.Format("删除{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();

            }
        }
        public void Delete(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            if (bc.CommandArgument == "select")
            {
                var idArrTemp = this.gvList.GetCheckKeys();
                if (idArrTemp.Length == 0)
                    throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
                var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
                Delete(idArr);
            }
            else
            {
                Delete(bc.CommandArgument.Convert<int>());
            }
        }
        public void Search(object sender, EventArgs e)
        {
            //this.gvList.RemoveAllFilterExpression();
            //if (this.tbUserGroupName.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("UserGroup.GroupName.Contains(@0)", this.tbUserGroupName.Text);
            //if (this.tbGroupNo.Text.EmptyString()!=null)
            //    this.gvList.AddFilterExpression("UserGroup.UserGroupNo.Contains(@0)", this.tbGroupNo.Text);
            //if (this.ddlUserGroupType.SelectedValue!="-1")
            //    this.gvList.AddFilterExpression("UserGroupType.UserGroupTypeId=@0", this.ddlUserGroupType.SelectedValue.Convert<int>());
            //if (this.ddlUserGroupStatus.SelectedValue != "-1")
            //    this.gvList.AddFilterExpression("UserGroup.UserGroupStatus=@0", (int)this.ddlUserGroupStatus.SelectedValue.Convert<UserGroupStatus>());
            //if (this.tbEndTime1.Text.EmptyString()!=null)
            //    this.gvList.AddFilterExpression("UserGroup.EndTime<=@0", this.tbEndTime1.Text.Convert<DateTime>());
            //if (this.tbEndTime2.Text.EmptyString()!=null)
            //    this.gvList.AddFilterExpression("UserGroup.EndTime>=@0", this.tbEndTime2.Text.Convert<DateTime>());

            ListBind();
        }
        public void AllList(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            ListBind();
        }
    }
}
